home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / Point.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  6.4 KB  |  209 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Point.java    1.22 98/09/21
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. import java.awt.geom.Point2D;
  18.  
  19. /**
  20.  * A point representing a location in (x, y) coordinate space, specified
  21.  * in integer precision.
  22.  *
  23.  * @version     1.22, 09/21/98
  24.  * @author     Sami Shaio
  25.  * @since       JDK1.0
  26.  */
  27. public class Point extends Point2D implements java.io.Serializable {
  28.     /**
  29.      * The <i>x</i> coordinate.
  30.      * If no <i>x</i> coordinate is set it will default to '0'.
  31.      *
  32.      * @serial
  33.      * @see getLocation()
  34.      * @see Move()
  35.      */
  36.     public int x;
  37.  
  38.     /**
  39.      * The <i>y</i> coordinate. 
  40.      * If no <i>y</i> coordinate is set it will default to '0'.
  41.      *
  42.      * @serial
  43.      * @see getLocation()
  44.      * @see Move()
  45.      */
  46.     public int y;
  47.  
  48.     /*
  49.      * JDK 1.1 serialVersionUID 
  50.      */
  51.     private static final long serialVersionUID = -5276940640259749850L;
  52.  
  53.     /**
  54.      * Constructs and initializes a point at the origin 
  55.      * (0, 0) of the coordinate space. 
  56.      * @since       JDK1.1
  57.      */
  58.     public Point() {
  59.     this(0, 0);
  60.     }
  61.  
  62.     /**
  63.      * Constructs and initializes a point with the same location as
  64.      * the specified <code>Point</code> object.
  65.      * @param       p a point.
  66.      * @since       JDK1.1
  67.      */
  68.     public Point(Point p) {
  69.     this(p.x, p.y);
  70.     }
  71.  
  72.     /**
  73.      * Constructs and initializes a point at the specified 
  74.      * (<i>x</i>, <i>y</i>) location in the coordinate space. 
  75.      * @param       x   the <i>x</i> coordinate.
  76.      * @param       y   the <i>y</i> coordinate.
  77.      */
  78.     public Point(int x, int y) {
  79.     this.x = x;
  80.     this.y = y;
  81.     }
  82.  
  83.     /**
  84.      * Returns the X coordinate of the point in double precision.
  85.      */
  86.     public double getX() {
  87.     return x;
  88.     }
  89.  
  90.     /**
  91.      * Returns the Y coordinate of the point in double precision.
  92.      */
  93.     public double getY() {
  94.     return y;
  95.     }
  96.  
  97.     /**
  98.      * Returns the location of this point.
  99.      * This method is included for completeness, to parallel the
  100.      * <code>getLocation</code> method of <code>Component</code>.
  101.      * @return      a copy of this point, at the same location.
  102.      * @see         java.awt.Component#getLocation
  103.      * @see         java.awt.Point#setLocation(java.awt.Point)
  104.      * @see         java.awt.Point#setLocation(int, int)
  105.      * @since       JDK1.1
  106.      */
  107.     public Point getLocation() {
  108.     return new Point(x, y);
  109.     }    
  110.  
  111.     /**
  112.      * Sets the location of the point to the specificed location.
  113.      * This method is included for completeness, to parallel the
  114.      * <code>setLocation</code> method of <code>Component</code>.
  115.      * @param       p  a point, the new location for this point.
  116.      * @see         java.awt.Component#setLocation(java.awt.Point)
  117.      * @see         java.awt.Point#getLocation
  118.      * @since       JDK1.1
  119.      */
  120.     public void setLocation(Point p) {
  121.     setLocation(p.x, p.y);
  122.     }    
  123.  
  124.     /**
  125.      * Changes the point to have the specificed location.
  126.      * <p>
  127.      * This method is included for completeness, to parallel the
  128.      * <code>setLocation</code> method of <code>Component</code>.
  129.      * Its behavior is identical with <code>move(int, int)</code>.
  130.      * @param       x  the <i>x</i> coordinate of the new location.
  131.      * @param       y  the <i>y</i> coordinate of the new location.
  132.      * @see         java.awt.Component#setLocation(int, int)
  133.      * @see         java.awt.Point#getLocation
  134.      * @see         java.awt.Point#move(int, int)
  135.      * @since       JDK1.1
  136.      */
  137.     public void setLocation(int x, int y) {
  138.     move(x, y);
  139.     }    
  140.  
  141.     /**
  142.      * Sets the location of this point to the specified float coordinates.
  143.      */
  144.     public void setLocation(double x, double y) {
  145.     this.x = (int) Math.round(x);
  146.     this.y = (int) Math.round(y);
  147.     }
  148.  
  149.     /**
  150.      * Moves this point to the specificed location in the 
  151.      * (<i>x</i>, <i>y</i>) coordinate plane. This method
  152.      * is identical with <code>setLocation(int, int)</code>.
  153.      * @param       x  the <i>x</i> coordinate of the new location.
  154.      * @param       y  the <i>y</i> coordinate of the new location.
  155.      * @see         java.awt.Component#setLocation(int, int)
  156.      */
  157.     public void move(int x, int y) {
  158.     this.x = x;
  159.     this.y = y;
  160.     }    
  161.  
  162.     /**
  163.      * Translates this point, at location (<i>x</i>, <i>y</i>), 
  164.      * by <code>dx</code> along the <i>x</i> axis and <code>dy</code> 
  165.      * along the <i>y</i> axis so that it now represents the point 
  166.      * (<code>x</code> <code>+</code> <code>dx</code>, 
  167.      * <code>y</code> <code>+</code> <code>dy</code>). 
  168.      * @param       dx   the distance to move this point 
  169.      *                            along the <i>x</i> axis.
  170.      * @param       dy    the distance to move this point 
  171.      *                            along the <i>y</i> axis.
  172.      */
  173.     public void translate(int x, int y) {
  174.     this.x += x;
  175.     this.y += y;
  176.     }    
  177.  
  178.     /**
  179.      * Determines whether two points are equal. Two instances of
  180.      * <code>Point</code> are equal if the values of their 
  181.      * <code>x</code> and <code>y</code> member fields, representing
  182.      * their position in the coordinate space, are the same.
  183.      * @param      obj   an object to be compared with this point.
  184.      * @return     <code>true</code> if the object to be compared is
  185.      *                     an instance of <code>Point</code> and has
  186.      *                     the same values; <code>false</code> otherwise.
  187.      */
  188.     public boolean equals(Object obj) {
  189.     if (obj instanceof Point) {
  190.         Point pt = (Point)obj;
  191.         return (x == pt.x) && (y == pt.y);
  192.     }
  193.     return super.equals(obj);
  194.     }
  195.  
  196.     /**
  197.      * Returns a string representation of this point and its location 
  198.      * in the (<i>x</i>, <i>y</i>) coordinate space. This method is 
  199.      * intended to be used only for debugging purposes, and the content 
  200.      * and format of the returned string may vary between implementations. 
  201.      * The returned string may be empty but may not be <code>null</code>.
  202.      * 
  203.      * @return  a string representation of this point.
  204.      */
  205.     public String toString() {
  206.     return getClass().getName() + "[x=" + x + ",y=" + y + "]";
  207.     }
  208. }
  209.